package com.njxzc.igo.controller.shoppingcart;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.njxzc.igo.model.ShoppingCart;
import com.njxzc.igo.model.User;
import com.njxzc.igo.service.ShoppingCartService;
import com.njxzc.igo.service.ShoppingCartServiceImpl;

@WebServlet(name="reloadShoppingCart",urlPatterns="/myshoppingCart.do")
public class ReloadShoppingCart extends HttpServlet{
	private ShoppingCartService scs = new ShoppingCartServiceImpl();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session  = request.getSession();
		User u = (User) session.getAttribute("u");
		if(u!=null){
			Map shoppingMap = reloadShoppingCart(u);
			session.setAttribute("map3", shoppingMap);
		}
		request.getRequestDispatcher("WEB-INF/cart.jsp").forward(request, response);
	}
	
	
	protected Map reloadShoppingCart(User u){

		List<ShoppingCart> shoppingCart = scs.loadmyCart(u.getUsername());
		Map shoppingMap = scs.GroupBySales(shoppingCart);
		return shoppingMap;
	} 
	
}
